package org.example.java.base;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;

/**
 * User: ZhangJuntao
 * Date: 14-2-14
 * Time: 上午9:32
 */
public class SerializableTest {
  private static final String path = "output/base/transient/logInfo.serial";

  static class LoggingInfo implements java.io.Serializable {
    private Date loggingDate = new Date();
    private String uid;
    private transient String pwd;

    LoggingInfo(String user, String password) {
      uid = user;
      pwd = password;
    }

    public Date getLoggingDate() {
      return loggingDate;
    }

    public void setLoggingDate(Date loggingDate) {
      this.loggingDate = loggingDate;
    }

    public String getUid() {
      return uid;
    }

    public void setUid(String uid) {
      this.uid = uid;
    }

    public String getPwd() {
      return pwd;
    }

    public void setPwd(String pwd) {
      this.pwd = pwd;
    }

    @Override
    public String toString() {
      return "LoggingInfo{" +
          "loggingDate=" + loggingDate +
          ", uid='" + uid + '\'' +
          ", pwd='" + pwd + '\'' +
          '}';
    }
  }

  public static void main(String[] args) {
    LoggingInfo logInfo = new LoggingInfo("mike", "mechanics");
    System.out.println(logInfo.toString());
    try {
      ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream(path));
      o.writeObject(logInfo);
      o.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
    try {
      ObjectInputStream in = new ObjectInputStream(new FileInputStream(path));
      logInfo = (LoggingInfo) in.readObject();
      System.out.println(logInfo.toString());
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
